package com.changpei.search.controller;

import com.alibaba.fastjson.JSONObject;
import com.changpei.common.result.Result;
import com.changpei.search.pojo.CommentIndex;
import com.changpei.search.service.CommentIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 * <p>Title : CommentIndex</p>
 * <p>Description : commentIndex的控制层</p>
 * @author weibiao
 * @date 2020年3月7日
 */
@RestController
@RequestMapping("/commentIndex")
@CrossOrigin
public class CommentIndexController {

    @Autowired
    private CommentIndexService commentIndexService;

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增CommentIndex数据</p>
     * @param commentIndex
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody(required = false) CommentIndex commentIndex) {
        // 调用commentIndexService实现添加CommentIndex
        return commentIndexService.save(commentIndex);
    }

    /**
     *
     * <p>Title : delete</p>
     * <p>Description : CommentIndex根据ID主键删除数据</p>
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable(required = false) Long id) {
        // 调用commentIndexService实现根据主键删除
        return commentIndexService.deleteById(id);
    }

    /**
     *
     * <p>Title : update</p>
     * <p>Description : CommentIndex根据ID修改</p>
     * @param commentIndex
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody CommentIndex commentIndex, @PathVariable Long id) {
        // 设置主键值
        commentIndex.setId(id);
        // 调用commentIndexService实现修改CommentIndex
        return commentIndexService.update(commentIndex);
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : CommentIndex分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/{pageNum}/{pageSize}")
    public Result<Page<CommentIndex>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用commentIndexService实现分页查询CommentIndex
        Page<CommentIndex> commentIndices = commentIndexService.search(null, pageNum, pageSize);
        return Result.ok(commentIndices);
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : CommentIndex分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/{pageNum}/{pageSize}")
    public Result<Page<CommentIndex>> findPage(@RequestBody(required = false) CommentIndex commentIndex, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用commentIndexService实现分页查询CommentIndex
        Page<CommentIndex> pageInfo = commentIndexService.search(commentIndex, pageNum, pageSize);
        return Result.ok(pageInfo);
    }

    /**
     * 按照contentId查询
     * @param contentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/findByContentId")
    public Result<Map<String, Object>> findByContentId(@RequestBody Map<String, Object> map) {
        // 调用commentIndexService实现分页查询CommentIndex
        Map<String, Object> commentIndices = commentIndexService.findByContentId(map);
        return Result.ok(commentIndices);
    }

    /**
     * 查看回复详情
     * @param commentId
     * @return
     */
    @PostMapping(value = "/findCommentDetail")
    public Result<JSONObject> findCommentDetail(@RequestBody Map<String, Object> map) {
        JSONObject commentIndex = commentIndexService.findCommentDetail(map);
        return Result.ok(commentIndex);
    }

    /**
     * 查看回复详情子集
     * @param map
     * @return
     */
    @PostMapping(value = "/findCommentDetailChildren")
    public Result<Map<String, Object>> findCommentDetailChildren(@RequestBody Map<String, Object> map) {
        Map<String, Object> commentIndices = commentIndexService.findCommentDetailChildren(map);
        return Result.ok(commentIndices);
    }
}
